package com.haiertvbic.timeline.xml;

import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.haiertvbic.timeline.xml.handler.CommContentHandler;

public class XmlParse {

	private SAXParserFactory saxParserFactory;
	private XMLReader xmlReader;

	private static XmlParse instance;

	private XmlParse() {
	}

	public static synchronized XmlParse getXmlParse() {
		if (instance == null) {
			instance = new XmlParse();
		}
		return instance;
	}

	public synchronized List<?> parse(String xmlSrc, CommContentHandler handler) {
		List<?> infos = handler.getmInfos();
		try {
			setXMLReader();
			xmlReader.setContentHandler(handler);
			xmlReader.parse(new InputSource(new StringReader(xmlSrc)));
		} catch (Exception e) {
			infos = null;
		}
		return infos;
	}

	private void setXMLReader() throws Exception {
		if (xmlReader == null) {
			saxParserFactory = SAXParserFactory.newInstance();
			xmlReader = saxParserFactory.newSAXParser().getXMLReader();
		}
	}

}
